<?php

namespace App\Services;

use App\Models\HallGameConfig;
use App\Models\HallPartnerConfig;
use App\Models\User;
use App\Models\WebsiteBase;
use common\utils\SysCode;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\WebsiteAdvert;
use App\Models\Partner;
use App\Models\Notice;
use App\Models\Message;


//首页service
class HomeService
{

    const HOME_TYPE = [
        [
            'id' => 1,
            'name' => '真人视讯',
            'alias' => 'live'
        ],
        [
            'id' => 2,
            'name' => '体育博弈',
            'alias' => 'sports'
        ],
        [
            'id' => 3,
            'name' => '彩票游戏',
            'alias' => 'lottery'
        ],
        [
            'id' => 4,
            'name' => '电子游艺',
            'alias' => 'egame'
        ],
        [
            'id' => 5,
            'name' => '电竞竞猜',
            'alias' => 'esport'
        ]
    ];


    /**
     * 获取游戏菜单 四格
     */
    public static function getHnavigation()
    {
        $data = self::HOME_TYPE;
        $result = Redis::get(RedisKeys::SET_GLOBAL);
        $navigation = $data ? json_decode($result, true) : false;
        if (!empty($navigation)) {
            foreach ($navigation['menu'] as $k1 => $v1) {
                foreach ($data as $k2 => $v2) {
                    if (strcasecmp($v1['id'], $v2['alias']) == 0 && $v1['val'] == false) {
                        unset($data[$k2]);
                    }
                    if (!in_array($v2['alias'], array_column($navigation['menu'], 'id'))) {
                        unset($data[$k2]);
                    }
                }
            }
        }
        foreach ($data as $km => $vm) {
            if ($vm['alias'] == 'lottery') {
                $data[$km]['alias'] = 'lotteryHall';
            }
        }
        return array_values($data);
    }

    //获取首页浮动图
    public static function getFloatList($type)
    {
        $where = [
            ['type', '=', 2],
            ['platform', '=', (string)$type],
            ['status', '=', 1]
        ];
        $select = ["link", "picture", "position", "offset_x", "offset_y"];
        $list = WebsiteAdvert::where($where)->select($select)->get();
        $newList = [];
        if ($list) {
            foreach ($list as $k => $v) {
                $newList[$k]['pic'] = $v['picture'];
                $newList[$k]['position'] = $v['position'];
                $newList[$k]['link'] = $v['link'];
                $newList[$k]['offset_x'] = $v['offset_x'];
                $newList[$k]['offset_y'] = $v['offset_y'];
                $newList[$k]['language'] = 1;
            }
        }
        return $newList;
    }

    //首页banner图
    public static function getBannerList($condition, $select = ["link", "picture"])
    {
        $where = [
            ['position', '=', $condition['position']],
            ['platform', '=', $condition['pf']],
            ['type', '=', $condition['type']],
            ['status', '=', 1]
        ];

        $model = WebsiteAdvert::where($where);
        if (isset($conditio['id'])) {
            $model->where('id', '=', $condition['id']);
        }

        $model->select($select)->orderBy('sort');

        $data = $model->get();
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['picture'] = $v['picture'];
                $data[$k]['link'] = $v['link'];
                $data[$k]['language'] = 'zh';
            }
        }
        return $data;
    }

    /**
     * pc 获取首页相关数据 egames  menu 首页 电子游艺
     */
    public static function getMenuIndex()
    {
        $data = static::getIndexData();
        $mw = $pt = $hb = [];
        $data['egames'][0]['info'] = $mw;
        $data['egames'][1]['info'] = $hb;
        $data['egames'][2]['info'] = $pt;

        //获取数据
        //视讯类菜单
        $live_menu = HomeService::getLiveMenu();

        //电子游艺
        $egame_menu = HomeService::getEgameMenu();


        //获取每个彩票最近一期的开奖结果
        $period_result = [];
//        $period_result  = HomeService::lotteryPeriodCode();

        //视讯
        $data['menu'][0]['info'] = !empty($live_menu) ? $live_menu : [];
        //电子游艺
        $data['menu'][1]['info'] = isset($egame_menu) ? $egame_menu : [];
        //彩票
        $data['menu'][2]['info'] = !empty($lottery_menu) ? $lottery_menu : [];
        $data['menu'][2]['period_code'] = !empty($period_result) ? $period_result : [];
        $data['menu'][2]['list'] = !empty($lottery_list) ? $lottery_list : [];
        foreach ($data['menu'] as $k => &$v) {
            if ($v['name'] == 'lottery') {
                $v['name'] = 'lotteryHall';
                break;
            }
        }
        return $data;
    }

    /**
     * home首页数据
     * @return mixed
     */
    public static function getIndexData()
    {
        $data = "{
    \"menu\": [
        {
            \"name\": \"live\",
            \"value\": \"真人视讯\",
            \"bg_img\": \"/static/index/main_yewu_bg_01.png\",
            \"hd_img\": \"/static/index/main_yewu_user_01.png\",
            \"description\": \"汇聚全球顶级真人视讯平台\",
            \"info\": [
                {
                    \"name\": \"live\",
                    \"value\": \"DG梦幻厅\",
                    \"open\": 1,
                    \"href\": \"\"
                },
                {
                    \"name\": \"live\",
                    \"value\": \"AG视讯厅\",
                    \"open\": 1,
                    \"href\": \"\"
                },
                {
                    \"name\": \"live\",
                    \"value\": \"BG视讯厅\",
                    \"open\": 1,
                    \"href\": \"\"
                },
                {
                    \"name\": \"live\",
                    \"value\": \"BBIN视讯厅\",
                    \"open\": 1,
                    \"href\": \"\"
                },
                {
                    \"name\": \"live\",
                    \"value\": \"MG视讯厅\",
                    \"open\": 1,
                    \"href\": \"\"
                },
                {
                    \"name\": \"live\",
                    \"value\": \"LB视讯厅\",
                    \"open\": 1,
                    \"href\": \"\"
                },
                {
                    \"name\": \"live\",
                    \"value\": \"OG视讯厅\",
                    \"open\": 1,
                    \"href\": \"\"
                }
            ]
        },
        {
            \"name\": \"egame\",
            \"value\": \"电子游艺\",
            \"bg_img\": \"/static/index/main_yewu_bg_02.png\",
            \"hd_img\": \"/static/index/main_yewu_user_02.png\",
            \"description\": \"精心打造的游戏画面让用户流连忘返\",
            \"info\": [
                {
                    \"name\": \"egame\",
                    \"value\": \"PT\",
                    \"open\": 1,
                    \"href\": \"https://www.baidu.com/\"
                }
            ]
        },
        {
            \"name\": \"sports\",
            \"value\": \"体育博弈\",
            \"bg_img\": \"/static/index/main_yewu_bg_04.png\",
            \"hd_img\": \"/static/index/main_yewu_user_03.png\",
            \"description\": \"每日提供上百场精彩体育赛事\",
            \"info\": [
                {
                    \"name\": \"sports\",
                    \"value\": \"足球\",
                    \"open\": 2,
                    \"href\": \"/sports\"
                },
                {
                    \"name\": \"sports\",
                    \"value\": \"篮球\",
                    \"open\": 2,
                    \"href\": \"/sports\"
                },
                {
                    \"name\": \"sports\",
                    \"value\": \"橄榄球\",
                    \"open\": 2,
                    \"href\": \"/sports\"
                },
                {
                    \"name\": \"sports\",
                    \"value\": \"赛车\",
                    \"open\": 2,
                    \"href\": \"/sports\"
                },
                {
                    \"name\": \"sports\",
                    \"value\": \"电子竞技\",
                    \"open\": 2,
                    \"href\": \"/sports\"
                }
            ]
        }
    ],
    \"egames\": [
    	{
            \"id\": \"1\",
            \"name\": \"MW\",
            \"img\": \"/static/index/egame/mw.png\",
            \"info\": [
                {
                    \"id\": 1,
                    \"img\": \"/static/egame/longgong.png\",
                    \"name\": \"龙宫\",
                    \"enter\": \"https://www.baidu.com/\"
                }
            ]
        },
        {
            \"id\": \"2\",
            \"name\": \"HB\",
            \"img\": \"/static/index/egame/g-t1.png\",
            \"info\": [
                {
                    \"id\": 1,
                    \"img\": \"/static/egame/longgong.png\",
                    \"name\": \"龙宫\",
                    \"enter\": \"https://www.baidu.com/\"
                },
                {
                    \"id\": 2,
                    \"img\": \"/static/egame/bikini.png\",
                    \"name\": \"比基尼派对\",
                    \"enter\": \"https://www.baidu.com/\"
                },
                {
                    \"id\": 3,
                    \"img\": \"/static/egame/lion.png\",
                    \"name\": \"银狮奖\",
                    \"enter\": \"https://www.baidu.com/\"
                },
                {
                    \"id\": 4,
                    \"img\": \"/static/egame/luck.png\",
                    \"name\": \"幸运双星\",
                    \"enter\": \"https://www.baidu.com/\"
                },
                {
                    \"id\": 5,
                    \"img\": \"/static/egame/longgong.png\",
                    \"name\": \"龙宫\",
                    \"enter\": \"https://www.baidu.com/\"
                },
                {
                    \"id\": 6,
                    \"img\": \"/static/egame/bikini.png\",
                    \"name\": \"比基尼派对\",
                    \"enter\": \"https://www.baidu.com/\"
                }
            ]
        },
        {
            \"id\": \"3\",
            \"name\": \"PT\",
            \"img\": \"/static/index/egame/mw.png\",
            \"info\": [
                {
                    \"id\": 1,
                    \"img\": \"/static/egame/longgong.png\",
                    \"name\": \"龙宫\",
                    \"enter\": \"https://www.baidu.com/\"
                }
            ]
        }
    ]
}";
        return json_decode($data, true);
    }

    /**
     * 获取视讯菜单
     * @return array
     */
    public static function getLiveMenu()
    {
        //$protocol = static::getProtocol();
        $live_list = static::getGameGlobalSet();
        if (!$live_list) {
            return [];
        }
        $live_menu = [
            [
                "name" => "live",
                "alias" => "LEBO",
                "game_name" => "LEBO",
                "value" => "LEBO视讯",
                "open" => 2,
                "enter" => "/game/third?g=Lebo&c=authorization",
                "href" => "/game/third?g=Lebo&c=authorization",
                "test_href" => "/game/third?g=Lebo&c=authorization"
            ],
            [
                "name" => "live",
                "alias" => "AG",
                "game_name" => "AG",
                "value" => "AG视讯",
                "open" => 2,
                "enter" => "/game/third?g=ag&c=forwardGame",
                "href" => "/game/third?g=ag&c=forwardGame",
                "test_href" => "/game/third?g=ag&c=forwardGame"
            ],
            [
                "name" => "live",
                "alias" => "BG",
                "game_name" => "BG",
                "value" => "BG视讯厅",
                "open" => 2,
                "enter" => "/game/third?g=Bg&c=gameUrl",
                "href" => "/game/third?g=Bg&c=gameUrl",
                "test_href" => ""
            ],
            [
                "name" => "live",
                "alias" => "OG",
                "game_name" => "OG",
                "value" => "OG视讯厅",
                "open" => 2,
                "enter" => "/game/third?g=Og&c=gameList",
                "href" => "/game/third?g=Og&c=gameList",
                "test_href" => ""
            ]
        ];
        return static::filterMenus($live_menu);
    }

    /**
     * 过滤
     * @param $game
     * @param $liveFilter 是否视讯游戏过滤
     * @return array
     */
    public static function filterMenus($game, $liveFilter = false)
    {
        $result = $result2 = [];
        $m = 0;
        $config = Redis::get(RedisKeys::SET_GLOBAL);
        if ($config) {
            $data = json_decode($config, true);
            if (isset($data['games'])) {
                foreach ($game as $k => $v) {
                    foreach ($data['games'] as $vv) {
                        if (strcasecmp($v['alias'], $vv['alias']) == 0) {
                            if ($vv['val']) {
                                $v['isOpen'] = true;
                                $result[$k] = $v;
                                if (!$liveFilter) {
                                    $result2[$m] = $v;
                                    $m++;
                                }
                                break;
                            }
                        }
                    }

                    if ($liveFilter) {
                        if (!isset($result[$k])) {
                            $v['isOpen'] = false;
                            $result[$k] = $v;
                        }
                    }
                }
            }
            return $liveFilter ? $result : $result2;
        }
        return $game;
    }

    /**
     * 电子游艺
     */
    public static function getEgameMenu()
    {
        $egame_list = static::getGameGlobalset();
        if (!$egame_list) {
            return [];
        }
        $egame_menu = [
            [
                "name" => "egame",
                "alias" => "FG",
                "value" => "FG电子",
                "game_name" => "FG电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=FG"
            ],
            [
                "name" => "egame",
                "alias" => "PT",
                "value" => "PT电子",
                "game_name" => "PT电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=PT"
            ],
            [
                "name" => "egame",
                "alias" => "AG2",
                "value" => "AG捕鱼王",
                "game_name" => "AG捕鱼王",
                "open" => 2,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/game/third?g=ag&c=forwardGame&type=6"
            ],
            [
                "name" => "egame",
                "alias" => "MW",
                "value" => "MW电子",
                "game_name" => "MW电子",
                "open" => 3,
                "img" => "/static/index/egame/mw.png",
                "href" => "/egame?game=MW"
            ],
            [
                "name" => "egame",
                "alias" => "HB",
                "value" => "HB电子",
                "game_name" => "HB电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=HB"
            ],
            [
                "name" => "egame",
                "alias" => "GNS",
                "value" => "GNS电子",
                "game_name" => "GNS电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=GNS"
            ],
            [
                "name" => "egame",
                "alias" => "AG1",
                "value" => "AG电子",
                "game_name" => "AG电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=AG1"
            ],
            [
                "name" => "egame",
                "alias" => "MG",
                "value" => "MG电子",
                "game_name" => "MG电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=MG"
            ],
            [
                "name" => "egame",
                "alias" => "CQ9",
                "value" => "CQ9电子",
                "game_name" => "CQ9电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=CQ9"
            ],
            [
                "name" => "egame",
                "alias" => "BNG",
                "value" => "BNG电子",
                "game_name" => "BNG电子",
                "open" => 3,
                "img" => "/static/index/egame/g-t1.png",
                "href" => "/egame?game=BNG"
            ],
        ];
        $new_egame_menu = [];
        foreach ($egame_menu as $k => $v) {
            $new_egame_menu[] = $v;
        }
        return static::filterMenus($new_egame_menu);
    }


    /**
     * 电子竞技
     */
    public static function getBiMenu()
    {
        $bi_menu = [
            [
                "name" => "esport",
                "alias" => "LOL",
                "value" => "英雄联盟",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=1"
            ],
            [
                "name" => "esport",
                "alias" => "WANGZHE",
                "value" => "王者荣耀",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=2"
            ],
            [
                "name" => "esport",
                "alias" => "DOTA2",
                "value" => "DOTA2",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=3"
            ],
            [
                "name" => "esport",
                "alias" => "CS:GO",
                "value" => "CS:GO",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=4"
            ],
            [
                "name" => "esport",
                "alias" => "JUEDI",
                "value" => "绝地求生",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=5"
            ],
            [
                "name" => "esport",
                "alias" => "SHOUWANG",
                "value" => "守望先锋",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=6"
            ],
            [
                "name" => "esport",
                "alias" => "STARWAR2",
                "value" => "星际争霸2",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=7"
            ],
            [
                "name" => "esport",
                "alias" => "MOSHOU3",
                "value" => "魔兽争霸3",
                "open" => 2,
                "img" => "",
                "href" => "/esport?gameId=8"
            ],
        ];
        return $bi_menu;
    }

    /**
     * 电子竞技
     */
    public static function getSportsMenu()
    {
        $vsports_menu = [
            [
                "name" => "sports",
                "alias" => "SB",
                "value" => "沙巴体育",
                "open" => 2,
                "img" => "",
                "href" => "/game/third?g=sb&c=play"
            ],
            [
                "name" => "sports",
                "alias" => "UG",
                "value" => "UG体育",
                "open" => 2,
                "img" => "",
                "href" => "/game/third?g=Ug&c=login"
            ],
            [
                "name" => "sports",
                "alias" => "3SING",
                "value" => "三昇体育",
                "open" => 2,
                "img" => "",
                "href" => "/game/third?g=Ss&c=play"
            ],
            [
                "name" => "sports",
                "alias" => "BTI",
                "value" => "BTI",
                "open" => 2,
                "img" => "",
                "href" => "/game/third?g=bti&c=play"
            ],
        ];
        return static::filterMenus($vsports_menu);
    }


    /**
     * 获取游戏全局设置
     */
    public static function getGameGlobalSet()
    {
        $global = static::globalData();
        //平台的第三方游戏列表
        $fields = 'id,name,is_live,is_egame,is_sport,is_lottery,is_card';
        $where = explode(",", $fields);
        $info = Partner::where('status', '=', 1)
            ->select($where)
            ->limit(100)
            ->get();

        //去掉平台中处于维护中的
        if ($info) {
            $info_data = $info;
            $pt_game_list = [];
            foreach ($info_data as $val) {
                array_push($pt_game_list, $val['name']);
            }
        } else {
            return [];
        }

        if (!$global || !isset($global['games']) || !$global['games']) return [];
        $live_list = $global['games'];
        $new_live_list = [];
        foreach ($live_list as $k => $v) {
            $new_live_list[$v['name']] = $v['id'];
        }
        return $new_live_list;
    }

    /**
     * @param array $gameList
     * @return bool|mixed
     */
    public static function globalData($gameList = [])
    {
        if (!($data = Redis::get(RedisKeys::SET_GLOBAL))) {
            return false;
        }
        $data = json_decode($data, true);
        $set = Redis::get(RedisKeys::GLOBAL_GAME_ID_MAP_GAME_TYPE);
        $set = json_decode($set, true);
        foreach ($data['games'] as &$game) {
            if (!isset($game['alias'])) {
                $game['alias'] = $set[$game['id']];
            }
        }
        $games = $data['games'];
        //$partner = $this->module->app()->module('common')->gameapi->partners() ?? [];
        $partner = $gameList;
        if ($partner) {
            $data['games'] = [];
            foreach ($partner as $record) {
                $find = null;
                foreach ($games as $game) {
                    if ($game['id'] == $record['id']) {
                        $find = $game;
                        break;
                    }
                }
                $data['games'][] = $find ? $find : ['id' => $record['id'], 'alias' => $record['alias'], 'name' => $record['name'], 'val' => true, 'is_hot' => false];
            }
        }
        return $data;
    }

    //获取视讯列表
    public static function getLiveList()
    {

        //接口所需数据
        $data = [
            'ad' => [],
            'list' => [],
            'items' => [],
        ];
        //获取视讯相关广告
        $condition = [
            'language_id' => 1,
            'type' => 'banner',
            'position' => 'live',
            'approve' => 'pass',
            'pf' => 'pc',
            'status' => 'enabled'
        ];

        $liveADList = static::getBannerList($condition);
        if ($liveADList) {
            $arr = [];
            foreach ($liveADList as $k => $v) {
                $arr[$k]['pic'] = $v['picture'];
                $arr[$k]['link'] = $v['link'];
                $arr[$k]['language'] = "zh";
            }
            $data['ad'] = $arr;
        }

        //第三方游戏id  partner 表
        $dg_id = 10;
        $bg_id = 8;
        $lebo_id = 15;
//        $gameSet = HomeService::getGameGlobalSet();

        //lebo
        $lebo['id'] = 'mainNav-ebet';
        $lebo['alias'] = 'LEBO';
        $lebo_enter = "/game/third?g=Lebo&c=authorization";
        $lebo["img"] = "/static/index/live/shixun01D.png";
        $lebo["logo"] = "/static/LB.png?4";
        $lebo["title"] = $lebo["name"] = "LB视讯";
        $lebo["text"] = "LEBOGAMING";
        $lebo['test_play'] = "";
        $lebo['enter'] = $lebo_enter;
        $lebo['game_name'] = 'LEBO';
        $lebo['platform_id'] = $lebo_id;
        $lebo['description'] = '利博视讯云集众多美女荷官让您在游戏的同时，享受身临其境般的乐趣，平台提供多元网路娱乐服务和游戏商品开发。';
        $data_list[] = $lebo;
        //AG
        $ag['id'] = 'mainNav-ag';
        $ag['alias'] = 'AG';
        $ag_enter = "/game/third?g=ag&c=forwardGame";
        $ag["img"] = "/static/index/live/shixun04.png";
        $ag["logo"] = "/static/index/egame/g-t3.png";
        $ag["title"] = $ag["name"] = "AG视讯";
        $ag["text"] = "ASIAGAMING";
        $ag['test_play'] = "";
        $ag['enter'] = $ag_enter;
        $ag['game_name'] = 'AG';
        $ag['platform_id'] = $bg_id;
        $ag['description'] = 'DS真人娱乐最专业权威的信誉博彩公司排名评级机构，玩法众多如真人百家乐，德州扑克、骰宝、龙虎斗、温州牌九等游戏。';
        $data_list[] = $ag;
        //BG
        $bg['id'] = 'mainNav-bg';
        $bg['alias'] = 'BG';
        $bg["img"] = "/static/index/live/shixun06.png";
        $bg["logo"] = "/static/index/egame/g-t7.png";
        $bg["title"] = $bg["name"] = "BG视讯";
        $bg["text"] = "BIGGAMING";
        $bg_enter = "/game/third?g=Bg&c=gameUrl";
        $bg_test_play = "/game/third?g=Bg&c=videoTrialGameUrl";
        $bg['test_play'] = $bg_test_play;
        $bg['enter'] = $bg_enter;
        $bg['game_name'] = 'BG';
        $bg['platform_id'] = $bg_id;
        $bg['description'] = '最具潜力业内新秀，创新的2D及3D版真人娱乐城游戏，包括备受推崇的百家乐、七喜百家乐、龙虎、骰宝、轮盘、二十一点、三公等。';
        $data_list[] = $bg;
        //bbin
        $bbin['id'] = 'mainNav-bbinGame';
        $bbin['alias'] = 'BBIN';
        $bbin["img"] = "/static/index/live/shixun03.png";
        $bbin["logo"] = "/static/index/egame/g-t4.png";
        $bbin["title"] = $bbin["name"] = "BBIN视讯";
        $bbin["text"] = "BBINGAMING";
        $bbin_enter = "/game/third?g=Bg&c=videoBbinGameUrl";
        $bbin['test_play'] = "";
        $bbin['enter'] = $bbin_enter;
        $bbin['game_name'] = 'BBIN';
        $bbin['platform_id'] = $bg_id;
        $bbin['description'] = 'BBIN是亚洲享有盛名的网路真人博彩娱乐集团，平台提供多元网路娱乐服务和游戏商品开发，无论是在运动博弈、真人视讯、电子游艺、桌上游戏、乐透彩等皆有丰富多样的选择。';
        $data_list[] = $bbin;
        //DG
        $dg['id'] = 'mainNav-dg';
        $dg['alias'] = 'DG';
        $dg["img"] = "/static/index/live/shixun01.png";
        $dg["logo"] = "/static/DG.png";
        $dg["title"] = $dg["name"] = "DG视讯";
        $dg["text"] = "DREAMGAMING";
        $dg_enter = "/game/third?g=Dg&c=login";
        $dg_test_play = "/game/third?g=Dg&c=free";
        $dg['test_play'] = $dg_test_play;
        $dg['enter'] = $dg_enter;
        $dg['game_name'] = 'DG';
        $dg['platform_id'] = $dg_id;
        $dg['description'] = '最佳业内在线游戏平台，创新的2D及3D版真人娱乐城游戏，最先进的实况直播技术为您的玩家提供最具陆地赌场感官的真实体验。';
        $data_list[] = $dg;
        //ab
        $ab['id'] = 'mainNav-ab';
        $ab['alias'] = 'AB';
        $ab["img"] = "/static/index/live/shixun02.png";
        $ab["logo"] = "/static/Allbet.png?4";
        $ab["title"] = $ab["name"] = "AB视讯";
        $ab["text"] = "ALLBETAMING";
        $bbin_enter = "/game/third?g=Bg&c=videoAllbetGameUrl";
        $ab['test_play'] = "";
        $ab['enter'] = $bbin_enter;
        $ab['game_name'] = 'AB';
        $ab['platform_id'] = $bg_id;
        $ab['description'] = '极具魅力双人荷官，包台、多台下注刺激不断，独有竞咪游戏，最红女优定期上线，赢钱机会绝不错过。';
        $data_list[] = $ab;

        //og
        $og['id'] = 'mainNav-og';
        $og['alias'] = 'OG';
        $og["img"] = "/static/index/live/shixun05.png";
        $og["logo"] = "/static/index/egame/g-t2.png";
        $og["title"] = $og["name"] = "OG视讯";
        $og["text"] = "ORIENTALGAME";
        $og_enter = "/game/third?g=Og&c=gameList";
        $og['test_play'] = "";
        $og['enter'] = $og_enter;
        $og['game_name'] = 'OG';
        $og['platform_id'] = $bg_id;
        $og['description'] = 'OG视讯用心了解玩家，玩家公认著名赌场品牌,各国荷官在线、特色抽牌方式带您领略色香味俱全的博彩游戏。';
        $data_list[] = $og;

        //首页图片img
        $items = [
            [
                'title' => 'LEBO',
                'alias' => 'LEBO',
                'img' => '/static/index/live/LEBO.png',
                'enter' => "/game/third?g=Lebo&c=authorization",
            ],
            [
                'title' => 'AG',
                'alias' => 'AG',
                'img' => '/static/index/live/AG.png',
                'enter' => "/game/third?g=ag&c=forwardGame",
            ],
            [
                'title' => 'BG',
                'alias' => 'BG',
                'img' => '/static/index/live/BG.png',
                'enter' => "/game/third?g=Bg&c=gameUrl",
            ],
            [
                'title' => 'BBIN',
                'alias' => 'BBIN',
                'img' => '/static/index/live/BBin.png',
                'enter' => "/game/third?g=Lebo&c=authorization",
            ],

            [
                'title' => 'OG',
                'alias' => 'OG',
                'img' => '/static/index/live/OG.png',
                'enter' => "/game/third?g=Og&c=gameList",
            ],
            [
                'title' => 'DS',
                'alias' => 'DS',
                'img' => '/static/index/live/DS.png',
                'enter' => "/game/third?g=Lebo&c=authorization",
            ],
        ];

        $data_list = HomeService::filterMenus($data_list, true);
        $items = HomeService::filterMenus($items, true);

        $data['items'] = $items;
        $data['list'] = $data_list;
        return $data;
    }

    //获取H5首页大类
    public static function getHomeMenuList()
    {

        $list = [
            ['type' => 'hot', 'code' => 1, 'name' => '热门游戏', 'sort' => 0, 'list' => []],
            ['type' => 'live', 'code' => 2, 'name' => '真人视讯', 'sort' => 1, 'list' => []],
            ['type' => 'egame', 'code' => 3, 'name' => '电子游艺', 'sort' => 2, 'list' => []],
            ['type' => 'sport', 'code' => 4, 'name' => '体育博弈', 'sort' => 3, 'list' => []],
            ['type' => 'esport', 'code' => 5, 'name' => '电子竞技', 'sort' => 4, 'list' => []],
            ['type' => 'lottery', 'code' => 6, 'name' => '彩票游戏', 'sort' => 5, 'list' => []],
            ['type' => 'card', 'code' => 7, 'name' => '棋牌游戏', 'sort' => 6, 'list' => []],
        ];
        $globalSetting = HelpService::getGlobalSetting();
        $partnerData = HallPartnerConfig::where('status', '=', 1)->get();

        foreach ($list as $ke => $va) {
            if (!empty($globalSetting) && is_array($globalSetting)) {
                foreach ($globalSetting['menu'] as $k => $v) {
                    if (strcasecmp($v['id'], $va['type']) == 0 && $v['val'] == false) {
                        unset($list[$ke]);
                    }
                }
            }
        }
        if (!empty($partnerData)) {
            foreach ($list as $ke => $va) {
                foreach ($partnerData as $k => $v) {
                    unset($v['game_config']);
                    if (!empty($v['is_hot']) && $v['is_hot'] == 1 && !empty($va['type']) && $va['type'] == 'hot') {
                        $list[$ke]['list'][] = $v;
                    }
                    if (!empty($v['has_live']) && $v['has_live'] == 1 && !empty($va['type']) && $va['type'] == 'live') {
                        $list[$ke]['list'][] = $v;
                    }
                    if (!empty($v['has_egame']) && $v['has_egame'] == 1 && !empty($va['type']) && $va['type'] == 'egame') {
                        $list[$ke]['list'][] = $v;
                    }
                    if (!empty($v['has_sport']) && $v['has_sport'] == 1 && !empty($va['type']) && $va['type'] == 'sport') {
                        $list[$ke]['list'][] = $v;
                    }
                    if (!empty($v['has_esport']) && $v['has_esport'] == 1 && !empty($va['type']) && $va['type'] == 'esport') {
                        $list[$ke]['list'][] = $v;
                    }
                    if (!empty($v['has_lottery']) && $v['has_lottery'] == 1 && !empty($va['type']) && $va['type'] == 'lottery') {
                        $list[$ke]['list'][] = $v;
                    }
                    if (!empty($v['has_card']) && $v['has_card'] == 1 && !empty($va['type']) && $va['type'] == 'card') {
                        $list[$ke]['list'][] = $v;
                    }
                }
            }
        }
        $result = [];
        foreach ($list as $k => $v) {
            if (empty($v['list'])) {
                unset($list[$k]);
                continue;
            }
            $result[$k] = $v;
            $result[$k]['list'] = [];
            foreach ($v['list'] as $ke => $va) {
                $result[$k]['list'][$ke]['id'] = $va['id'];
                $result[$k]['list'][$ke]['name'] = $va['partner_name'];
                $result[$k]['list'][$ke]['iconkey'] = strtolower($va['partner_name']);
                $result[$k]['list'][$ke]['hot'] = $va['is_hot'];
            }

        }
        return array_values($result);
    }


    //获取首页公告列表
    public static function getNoticeList($user, $type, $size, $status = 1, $sendType = [1, 2, 3])
    {
        //未登陆状态获取公告

        if (empty($user)) {
            $where = [
                ['popup_type', '=', $type],
                ['status', '=', $status],
                ['begin_time', '<=', date("Y-m-d H:i:s")],
                ['end_time', '>=', date("Y-m-d H:i:s")]
            ];
            $sqlObj = Notice::where($where)->select(['id', 'title', 'content', 'status', 'popup_type'])->whereIn('send_type', $sendType)
                ->orderBy('created_at', 'desc')->orderBy('id', 'desc');
            if (!empty($size)) {
                $sqlObj = $sqlObj->limit($size);
            }
            $rel = $sqlObj->get();
        } else {
            $result1 = $result2 = $result3 = [];

            $where = [
                ['popup_type', '=', $type],
                ['status', '=', $status],
                ['begin_time', '<=', date("Y-m-d H:i:s")],
                ['end_time', '>=', date("Y-m-d H:i:s")]
            ];
            foreach ($sendType as $t) {
                if ($t == 1) {//会员层级消息
                    $UserInfo = user::where('id', '=', $user['id'])->get();
                    $UserInfo = $UserInfo[0];
                    $level = $UserInfo['level_id'] ?? '';
                    $model = Notice::where($where)->select(['id', 'title', 'content', 'status', 'popup_type']);
//                    return [$model->toSql(),$where];
                    $model->whereRaw("find_in_set('{$level}', recipient)");
                    $model->where('send_type', '=', $t);
                    if (!empty($status)) {
                        $model->where('status', '=', $status);
                    }
                    $result1 = $model->orderBy('created_at', 'desc')->orderBy('id', 'desc')->get();
                    return $result1;
                } elseif ($t == 2) {//代理消息
                    $model = Notice::select(['id', 'title', 'content', 'lang', 'status', 'popup_type'])
                        ->where($where);
                    $model->andwhere(['send_type', '=', $t]);
                    $result2 = $model->orderBy('created_at desc, id desc')->get();
                } elseif ($t == 3) {//自定义会员名称消息
                    $userName = $user['username'] ?? '';
                    $model = Notice::select(['id', 'title', 'content', 'lang', 'status', 'popup_type'])
                        ->where($where);
                    $model->whereRaw("find_in_set('{$userName}', recipient)");
                    $model->andwhere(['=', 'send_type', $t]);
                    $result3 = $model->orderBy('created_at desc, id desc')->get();
                }
            }
            $rel = array_merge($result1, $result2, $result3);
        }
        return $rel;
    }

    //pc首页菜单
    public static function getHomeMenu()
    {
        $data = static::getDefaultMenu();
        $live_menu = HomeService::getLiveMenu();
        $egame_menu = HomeService::getEgameMenu();
        $bi_menu = HomeService::getBiMenu();
        $sports = HomeService::getSportsMenu();

        $agentKey = -1;
        foreach ($data as $k => $val) {
            switch ($val['name']) {
                case 'live':
                    $data[$k]['info'] = $live_menu;
                    break;
                case 'egame':
                    $data[$k]['info'] = $egame_menu;
                    break;
                case 'esport':
                    $data[$k]['info'] = $bi_menu;
                    break;
                case 'sports':
                    $data[$k]['info'] = $sports;
                    break;
                case 'agent':
                    $agentKey = $k;
                    break;
            }
        }
        return array_values($data);
    }

    //默认菜单
    public static function getDefaultMenu()
    {
        $str = '[
                    {"name":"index","value":"首页","open":3,"status":1},
                    {"name":"esport","value":"电竞竞猜","open":3,"status":1,"info":[]},
                    {"name":"live","value":"真人视讯","open":3,"status":1,"info":[{"name":"live","value":"DG梦幻厅","open":1,"img":"/static/DG.png","href":"https://www.baidu.com/"}]},
                    {"name":"lottery","value":"幸运彩票","open":3,"status":1,"info":[{"name":"lottery","value":"幸运28","open":3,"img":"/static/game-index/game_15.png","href":"/game/xy28"}]},
                    {"name":"sports","value":"体育竞猜","open":3,"status":1,"info":[]},
                    {"name":"egame","value":"电子游戏","open":3,"status":1,"info":[{"name":"egame","value":"PT","open":3,"img":"/static/index/egame/g-t1.png","href":"/egame"}]},
                    {"name":"sales","value":"优惠活动","open":3,"status":1},
                    {"name":"mobile_bet","value":"手机投注","open":3,"status":1},
                    {"name":"user","value":"个人中心","open":3,"status":1},
                    {"name":"agent","value":"合作加盟","open":2,"status":1}
                ]';
        $data = json_decode($str, true);
        return HomeService::filterMainMenus($data);
    }

    /**
     * 过滤
     * @param $mainMenu
     * @param $delGameMenu 是否删除过滤的菜单
     * @return array
     */
    public static function filterMainMenus($mainMenu, $delGameMenu = false)
    {
        $config = Redis::get(RedisKeys::SET_GLOBAL);
        if ($config) {
            $data = json_decode($config, true);
            if (isset($data['menu'])) {
                foreach ($mainMenu as $k => $v) {
                    foreach ($data['menu'] as $vv) {
                        if (strcasecmp($v['name'], $vv['id']) == 0) {
                            $mainMenu[$k]['status'] = $vv['val'] == true ? 1 : 0;
                        }
                        if (in_array($v['name'], ['sports', 'lottery', 'egame', 'live', 'esport']) && !in_array($v['name'], array_column($data['menu'], 'id'))) {
                            $mainMenu[$k]['status'] = 0;
                        }
                    }
                }
            }
        }
        return array_values($mainMenu);
    }


    //获取已读消息数字
    public static function getMessageRead($userID, $type = null)
    {
        $where = [
            ['p.status', '=', 0],
            ['m.status', '!=', 2],
            ['p.recipient_id', '=', $userID]
        ];
        $model = Message::from('message as m')->select(DB::raw('count(1) as num, m.type'))
            ->Join('message_record as p', 'm.id', '=', 'p.message_id')
            ->where($where);
        //根据消息类型分组
        if (!is_null($type)) {
            $model->where('m.type', '=', $type);//消息类型
        }
        $list = $model->groupBy('type')->get();

        $import_num = $general_num = 0;
        if ($list) {
            foreach ($list as $key => $val) {
                if ($val['type'] == 1) {//重要消息
                    $import_num = (int)$val['num'];
                }
                if ($val['type'] == 2) {//一般消息
                    $general_num = (int)$val['num'];
                }
            }
        }
        $number = $import_num + $general_num;
        $data = ['number' => $number, 'import_num' => $import_num, 'general_num' => $general_num];
        return $data;
    }

    //获取默认的二维码
    public static function getRrcode()
    {
        return ["url" => "/static/index/kefu/qrcode.png"];
    }

    //获取热门游戏
    public static function getHotGames()
    {
        $arry = [
            [
                'cid' => 1,
                'id' => 'mainNav-bi', // 识别
                'name' => '电子竞技',
                'alias' => 'BI', // 英文别名
                'enabled' => '1', // 是否开启
                'pc_id' => '', // 对应的游戏id
                'sort' => '3', // 排列

                'img' => 'static/images/platform/hot_hc_esport.png', // 图片地址，相对地址
                'link' => '/esports', // 二级菜单链接地址
                'url' => '/esports',
                'openType' => '0'
            ],
            [
                'cid' => 8,
                'id' => 'mainNav-ebet',
                'name' => 'LEBO视讯厅',
                'alias' => 'LEBO',
                'enabled' => '1',
                'pc_id' => '',
                'sort' => '1',

                'img' => 'static/images/platform/hot_lebo_live.png',
                'link' => '',
                'url' => "/gameapi?g=Lebo&c=authorization&loginType=2",
                'openType' => '1'
            ],
            [
                'cid' => 1,
                'id' => 'mainNav-sb',
                'name' => '沙巴体育',
                'alias' => 'SB',
                'enabled' => '1',
                'pc_id' => '13',
                'sort' => '8',

                'img' => 'static/images/platform/hot_sb_sport.png',
                'link' => '',
                'url' => '/gameapi?g=sb&c=mobileGame',
                'openType' => '1'
            ],
            [
                'cid' => 2,
                'id' => 'mainNav-ug',
                'name' => 'UG体育',
                'alias' => 'UG',
                'enabled' => '1',
                'pc_id' => '13',
                'sort' => '8',

                'img' => 'static/images/platform/hot_ug_sport.png',
                'link' => '',
                'url' => '/gameapi?g=Ug&c=mobileGame',
                'openType' => '1'
            ],
            [
                'cid' => 3,
                'id' => 'mainNav-og',
                'name' => 'OG视讯厅',
                'alias' => 'OG',
                'enabled' => '1',
                'pc_id' => '9',
                'sort' => '7',

                'img' => 'static/images/platform/hot_og_live.png',
                'link' => '',
                'url' => "/game/third?g=Og&c=gameList&isMobileUrl=1",
                'openType' => '1'
            ],
            [
                'cid' => 17,
                'id' => 'mainNav-mg',
                'name' => 'MG电子',
                'alias' => 'MG',
                'enabled' => '1',
                'pc_id' => '5',
                'sort' => '4',

                'img' => 'static/images/platform/hot_mg_dz.png',
                'link' => '/eRecreationDetail',
                'url' => "",
                'openType' => '0'
            ],
            // [
            //     'cid' => 6,
            //     'id' => 'mainNav-bbinGame',
            //     'name' => $language->bbin,
            //     'alias' => 'BBIN',
            //     'enabled' => '1',
            //     'pc_id' => '7',
            //     'sort' => '6',
            //     'img' => 'static/images/platform/bbin.png',
            //     'link' => '',
            //     'url' => "/gameapi?g=Bg&c=videoBbinGameUrl&isMobileUrl=1",
            //     'openType' => '1'
            // ],
            [
                'cid' => 7,
                'id' => 'mainNav-ag',
                'name' => 'AG视讯厅',
                'alias' => 'AG',
                'enabled' => '1',
                'pc_id' => '8',
                'sort' => '5',

                'img' => 'static/images/platform/hot_ag_live.png',
                'link' => '',
                'url' => "/gameapi?g=Ag&c=forwardGame",
                'openType' => '1'
            ],

            [
                'cid' => 9,
                'id' => 'mainNav-dg',
                'name' => 'DG视讯厅',
                'alias' => 'DG',
                'enabled' => '1',
                'pc_id' => '15',
                'sort' => '9',
                'img' => 'static/images/platform/dg.png',
                'link' => '',
                'url' => "/gameapi?g=Dg&c=loginh5&isMobileUrl=1",
                'openType' => '1'
            ],
            [
                'cid' => 10,
                'id' => 'mainNav-bg',
                'name' => 'BG视讯厅',
                'alias' => 'BG',
                'enabled' => '1',
                'pc_id' => '4',
                'sort' => '10',

                'img' => 'static/images/platform/hot_bg_live.png',
                'link' => '',
                'url' => "/gameapi?g=Bg&isMobileUrl=1&c=gameUrl",
                'openType' => '1'
            ],
            [
                'cid' => 11,
                'id' => 'mainNav-pt',
                'name' => 'PT电子',
                'alias' => 'PT',
                'enabled' => '1',
                'pc_id' => '17',
                'sort' => '11',

                'img' => 'static/images/platform/hot_pt_dz.png',
                'link' => '/eRecreationDetail',
                'url' => '',
                'openType' => '0',
            ],
            [
                'cid' => 12,
                'id' => 'mainNav-Hb',
                'name' => '哈巴电子',
                'alias' => 'Hb',
                'enabled' => '1',
                'pc_id' => '',
                'sort' => '12',

                'img' => 'static/images/platform/hot_hb_dz.png',
                'link' => '/eRecreationDetail',
                'url' => "",
                'openType' => '0'
            ],
            [
                'cid' => 15,
                'id' => 'mainNav-gns',
                'name' => 'GNS电子',
                'alias' => 'GNS',
                'enabled' => '1',
                'pc_id' => '',
                'sort' => '14',

                'img' => 'static/images/platform/hot_gns_dz.png',
                'link' => '/eRecreationDetail',
                'url' => "",
                'openType' => '0'
            ],
            [
                'cid' => 16,
                'id' => 'mainNav-fg',
                'name' => 'FG电子',
                'alias' => 'FG',
                'enabled' => '1',
                'pc_id' => '',
                'sort' => '15',

                'img' => 'static/images/platform/hot_fg_dz.png',
                'link' => '/eRecreationDetail',
                'url' => "",
                'openType' => '0'
            ],
            [
                'cid' => 18,
                'id' => 'mainNav-ag1',
                'name' => 'AG电子',
                'alias' => 'AG1',
                'enabled' => '1',
                'pc_id' => '18',
                'sort' => '18',

                'img' => 'static/images/platform/hot_ag_dz.png',
                'link' => '/eRecreationDetail',
                'url' => '',
                'openType' => '0'
            ],
            [
                'cid' => 19,
                'id' => 'mainNav-ag2',
                'name' => 'AG捕鱼王',
                'alias' => 'AG2',
                'enabled' => '1',
                'pc_id' => '19',
                'sort' => '19',

                'img' => 'static/images/platform/hot_ag_by.png',
                'link' => '',
                'url' => '/gameapi?g=Ag&c=forwardGame&type=6&token=',
                'openType' => '1'
            ],
            [
                'cid' => 20,
                'id' => 'mainNav-cq9',
                'name' => 'CQ9电子',
                'alias' => 'CQ9',
                'enabled' => '1',
                'pc_id' => '20',
                'sort' => '20',

                'img' => 'static/images/platform/hot_cq9_dz.png',
                'link' => '/eRecreationDetail',
                'url' => '',
                'openType' => '0'
            ],
            [
                'cid' => 21,
                'id' => 'mainNav-3SING',
                'name' => '三昇体育',
                'alias' => '3SING',
                'enabled' => '1',
                'pc_id' => '21',
                'sort' => '8',

                'img' => 'static/images/platform/hot_ss_sport.png',
                'link' => '',
                'url' => '/gameapi?g=Ss&c=mobileGame',
                'openType' => '1'
            ],

        ];

        $settingData = static::globalData();

        if (empty($settingData['games'])) {
            return [];
        };

        $result = [];
        foreach ($arry as $key => $value) {
            foreach ($settingData['games'] as $k => $v) {
                if ($value['id'] == $v['id'] && !empty($v['val']) && !empty($v['is_hot'])) {
                    $result[] = $value;
                }
            }
        }
        return $result;
    }

    public static function getDownLoadData()
    {
        $data = WebsiteBase::select(['qrcode_android','qrcode_ios'])->first()->toArray();
        return $data;
    }
}
